package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 排序字符串
 * @author XingHaiYang
 * @date 2022/9/29 14:43
 */
public class SortListDemo3 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("苍老师");
        list.add("范传奇");
        list.add("王克晶");
        list.add("三剑客");
        System.out.println(list);
//        Collections.sort(list);
//        System.out.println(list);
        //自定义一个比较器，就可以使String类不实现Comparable接口了
        Comparator<String> c = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
//                return o1.length()-o2.length();
                return o2.length()-o1.length();//反过来就是降序
            }
        };
        Collections.sort(list,c);
        System.out.println(list);
    }
}
